package com.ffcc.fitness.course.service.impl;

import com.ffcc.fitness.course.mapper.CourseMapper;
import com.ffcc.fitness.course.pojo.dto.CourseInsertParam;
import com.ffcc.fitness.course.pojo.dto.CourseUpdateParam;
import com.ffcc.fitness.course.pojo.entity.Course;
import com.ffcc.fitness.course.pojo.vo.CourseVO;
import com.ffcc.fitness.course.service.CourseService;
import com.ffcc.fitness.comment.service.CommentService;
import com.ffcc.fitness.card.pojo.vo.CommentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    
    @Autowired
    private CommentService commentService;

    @Override
    public List<CourseVO> select() {
        // 查询所有课程基本信息
        List<CourseVO> courseVOList = courseMapper.select();
        log.info("查出来的课程信息：" + courseVOList);

        // 为每个课程填充评论信息
        populateCourseDetails(courseVOList);

        return courseVOList;
    }
    
    /**
     * 为课程列表填充详细信息（评论数、评论内容）
     * 使用简化评论系统
     */
    private void populateCourseDetails(List<CourseVO> courseVOList) {
        for (CourseVO courseVO : courseVOList) {
            Long courseId = courseVO.getCourseId();

            // 使用新的评论服务查询评论数量
            // targetType = 2 表示课程
            Long commentCount = commentService.getCommentCount(courseId, 2);
            courseVO.setCommentCount(commentCount);

            // 查询评论内容
            List<CommentVO> commentVOList = commentService.getCommentsByTarget(courseId, 2);
            courseVO.setComments(commentVOList);
        }
    }

    @Override
    public void insert(CourseInsertParam courseInsertParam) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInsertParam, course);
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public void update(CourseUpdateParam courseUpdateParam) {
        Course course = new Course();
        BeanUtils.copyProperties(courseUpdateParam, course);
        course.setUpdateTime(LocalDateTime.now());
        
        int result = courseMapper.update(course);
        if (result == 0) {
            log.warn("更新课程失败，课程不存在或非本人创建，courseId: {}, coachId: {}", 
                    courseUpdateParam.getCourseId(), courseUpdateParam.getCoachId());
            throw new RuntimeException("更新失败，课程不存在或您没有权限更新此课程");
        }
        log.info("成功更新课程，courseId: {}, coachId: {}", 
                courseUpdateParam.getCourseId(), courseUpdateParam.getCoachId());
    }

    @Override
    public void delete(Long courseId, Long coachId) {
        int result = courseMapper.delete(courseId, coachId);
        if (result == 0) {
            log.warn("删除课程失败，课程不存在或非本人创建，courseId: {}, coachId: {}", courseId, coachId);
            throw new RuntimeException("删除失败，课程不存在或您没有权限删除此课程");
        }
        log.info("成功删除课程，courseId: {}, coachId: {}", courseId, coachId);
    }

}
